package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.MainDataManualSystemType;
import com.ctshk.common.enums.NumberConstant;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataAirSystemDTO;
import com.ctshk.rpc.system.entity.MainDataAirSystem;
import com.ctshk.rpc.system.entity.MainDataManualSystem;
import com.ctshk.rpc.system.mapper.MainDataAirSystemMapper;
import com.ctshk.rpc.system.mapper.MainDataManualSystemMapper;
import com.ctshk.rpc.system.req.MainDataAirSystemAddReq;
import com.ctshk.rpc.system.req.MainDataAirSystemListReq;
import com.ctshk.rpc.system.req.MainDataAirSystemUpdateReq;
import com.ctshk.rpc.system.service.IMainDataAirSystemService;
import com.ctshk.rpc.system.service.IMainDataManualSystemService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 机票外挂系统主数据表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-04-07
 */
@DubboService
public class MainDataAirSystemServiceImpl extends ServiceImpl<MainDataAirSystemMapper, MainDataAirSystem> implements IMainDataAirSystemService {
    @Resource
    private IMainDataManualSystemService mainDataManualSystemService;
    @Resource
    private MainDataManualSystemMapper mainDataManualSystemMapper;

    @Override
    public Result add(MainDataAirSystemAddReq req, TokenUser tokenUser) {
        MainDataAirSystem mainDataAirSystem = EntityUtil.copy(req, MainDataAirSystem.class);
        Long id = IdWorker.getId();
        mainDataAirSystem.setId(id);
        mainDataAirSystem.setGmtCreate(LocalDateTime.now());
        mainDataAirSystem.setCreatorId(tokenUser.getId() + "");
        mainDataAirSystem.setGmtModified(LocalDateTime.now());
        mainDataAirSystem.setCreatorName(tokenUser.getUsername());
        mainDataAirSystem.setStatus(1);
        mainDataAirSystem.setVersion(1);
        baseMapper.insert(mainDataAirSystem);
        return Result.success(id);
    }

    @Override
    public Result<PageResponse<MainDataAirSystemDTO>> list(MainDataAirSystemListReq req) {
        /*Page<MainDataAirSystem> page = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<MainDataAirSystem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(req.getSystemId()),MainDataAirSystem::getSystemId,req.getSystemId());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(req.getSupplierId()),MainDataAirSystem::getSupplierId,req.getSupplierId());

        IPage<MainDataAirSystem> iPage = baseMapper.selectPage(page,queryWrapper);
        if(CollectionUtil.isEmpty(iPage.getRecords())){
            return   Result.success(new PageResponse<MainDataAirSystemDTO>(new ArrayList<>(), iPage.getCurrent() == 1,iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()),
             iPage.getTotal(), iPage.getCurrent(), iPage.getSize()));
        }
        List<MainDataAirSystemDTO> mainDataAirSystemDTOList = EntityUtil.copyList(iPage.getRecords(), MainDataAirSystemDTO.class);
        return   Result.success(new PageResponse<MainDataAirSystemDTO>(mainDataAirSystemDTOList, iPage.getCurrent() == 1,iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize
        ()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize()));*/
        /****************************改调用统一的外挂系统************************/
        /*MainDataManualSystemQueryReq manualSystemQueryReq = new MainDataManualSystemQueryReq();
        manualSystemQueryReq.setType(2);
        manualSystemQueryReq.setPageNo(req.getPageNo());
        manualSystemQueryReq.setPageSize(req.getPageSize());
        Result<PageResponse<MainDataManualSystemDTO>> responseResult = mainDataManualSystemService.queryList(manualSystemQueryReq);
        Assert.isTrue(responseResult.isSuccess(), SystemError.getDefined(responseResult.getCode()));
        PageResponse<MainDataManualSystemDTO> pageData = responseResult.getData();
        List<MainDataManualSystemDTO> records = pageData.getRecords();
        List<MainDataAirSystemDTO> list = EntityUtil.copyList(records, MainDataAirSystemDTO.class);
        return Result.success(new PageResponse<>(list, pageData.getIsFirst(),
                pageData.getIsLast(), pageData.getTotal(), pageData.getPageNo(), pageData.getPageSize()));*/
        IPage<MainDataAirSystemDTO> page = mainDataManualSystemMapper.selectPage(MainDataAirSystemDTO.class, req,
                Wrappers.<MainDataManualSystem>lambdaQuery()
                        .eq(MainDataManualSystem::getType, MainDataManualSystemType.AIRPORT.getCode())
                        .eq(MainDataManualSystem::getStatus, NumberConstant.ONE.getValue())
                        .eq(MainDataManualSystem::getIsDeleted, NumberConstant.ZERO.getValue())
        );
        return Result.success(new PageResponse<>(page.getRecords(), null, null, page.getTotal(), page.getCurrent(), page.getSize()));
    }

    @Override
    public Result<MainDataAirSystemDTO> info(Long id) {
        MainDataAirSystem mainDataAirSystem = baseMapper.selectById(id);
        if (null == mainDataAirSystem) {
            return Result.failed(SystemError.SYS_411);
        }
        MainDataAirSystemDTO mainDataAirSystemDTO = EntityUtil.copy(mainDataAirSystem, MainDataAirSystemDTO.class);
        return Result.success(mainDataAirSystemDTO);
    }

    @Override
    public Result updateStatus(Long id) {
        MainDataAirSystem mainDataAirSystem = baseMapper.selectById(id);
        if (null == mainDataAirSystem) {
            return Result.failed(SystemError.SYS_411);
        }
        UpdateWrapper<MainDataAirSystem> updateWrapper = new UpdateWrapper();
        updateWrapper.lambda().eq(MainDataAirSystem::getId, id);
        if (mainDataAirSystem.getStatus().intValue() == NumberConstant.ONE.getValue().intValue()) {
            updateWrapper.lambda().set(MainDataAirSystem::getStatus, NumberConstant.ZERO.getValue());
        } else {
            updateWrapper.lambda().set(MainDataAirSystem::getStatus, NumberConstant.ONE.getValue());
        }
        baseMapper.update(null, updateWrapper);
        return Result.success(id);
    }

    @Override
    public Result update(MainDataAirSystemUpdateReq req, TokenUser tokenUser) {
        QueryWrapper<MainDataAirSystem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataAirSystem::getSupplierId, req.getSupplierId());
        queryWrapper.lambda().eq(MainDataAirSystem::getSystemId, req.getSystemId());
        MainDataAirSystem mainDataAirSystem = baseMapper.selectOne(queryWrapper);
        if (null != mainDataAirSystem && !req.getId().equals(mainDataAirSystem.getId())) {
            return Result.failed(SystemError.SYS_421);
        }
        MainDataAirSystem updateData = EntityUtil.copy(req, MainDataAirSystem.class);
        updateData.setGmtModified(LocalDateTime.now());
        baseMapper.updateById(updateData);
        return Result.success(req.getId());
    }
}
